<template>
    <div class="login_container">
        <div class="login_box">
            <div class="avatar_box">
                <!--头像-->
                <img src="../assets/logo.png" alt />
            </div>
            <!--添加表单-->
            <el-form
                    ref="loginFormRef"
                    :model="loginForm"
                    :rules="loginRules"
                    class="login_form"
                    label-width="0px"
            >
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="iconfont icon-ziyuanxhdpi"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" prefix-icon="iconfont icon-mima" type="password"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetLoginForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                loginForm: {
                    username: "admin",
                    password: "admin"
                },
                loginRules: {
                    username: [
                        { required: true, message: "请输入用户名", trigger: "blur" },
                        { min: 5, max: 8, message: "长度在 5 到 8 个字符", trigger: "blur" }
                    ],
                    password: [
                        { required: true, message: "请输入密码", trigger: "blur" },
                        { min: 1, max: 8, message: "密码为 1~8 位", trigger: "blur" }
                    ]
                }
            };
        },
        methods: {
            resetLoginForm() {
                this.$refs.loginFormRef.resetFields();
            },
            login() {
                this.$refs.loginFormRef.validate(async valid => {
                    if (!valid) return;
                    // 调用get请求
                    this.$http({
                        url: this.$http.adornUrl('/oauth/token?client_id=client&client_secret=secret&grant_type=password'),
                        method: 'get',
                        params: this.$http.adornParams({
                           'username': this.loginForm.username,
                           'password': this.loginForm.password
                        })
                    }).then(res=>{

                        console.log(res.data.access_token)
                        let jwt="Bearer "+res.data.access_token
                        sessionStorage.setItem("jwt",jwt)
                        this.$http({
                            url: this.$http.adornUrl('/yunxi/user/info/name?username='+this.loginForm.username),
                            method: 'get',
                        }).then(res=>{
                            sessionStorage.setItem("id",res.data.user.id)
                            sessionStorage.setItem("status",res.data.user.status)
                            this.$message.success("登陆成功！！！");
                            this.$router.push({ path: "/home"});
                        })

                    }).catch(res=>{
                        this.$message.error("登录失败！！！");
                    })


                });
            }
        }
    };
</script>

<style lang="less" scoped>

    // 跟节点样式
    .login_container {
        background-image: linear-gradient(120deg,#3498db,#8e44ad);
        height: 100%;
    }
    // 登录框
    .login_box {
        width: 450px;
        height: 300px;
        background-color: #fff;
        border-radius: 3px;// 圆角
        position: absolute;// 绝对定位
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);// 根据自己位置 以自己为长度位移
        // 头像框
        .avatar_box {
            width: 130px;
            height: 130px;
            border: 1px solid #eee;
            border-radius: 50%; // 加圆角
            padding: 10px;
            box-shadow: 0 0 10px #ddd;// 盒子阴影
            position: absolute;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: #0ee;
            img {
                width: 100%;
                height: 100%;
                border-radius: 50%; // 加圆角
                background-color: #eee;
            }
        }
        .btns {
            display: flex;// 弹性布局
            justify-content: flex-end; // 尾部对齐
        }
        .login_form {
            position: absolute;
            bottom: 0%;
            width: 100%;
            padding: 0 10px;
            box-sizing: border-box;// 设置边框
        }
    }
</style>
